<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\SArticleInfo as ArticleModel;
use App\Models\SAdviceInfo as AdviceModel;
use App\Models\SCloudLessonInfo as CloudLessonModel;
use App\Models\SPictureInfo as PictureModel;
use App\Models\SLessonInfo as LessonModel;
use App\Models\SStuCollectInfo as StuCollectModel;
use Illuminate\Support\Carbon;


class UserController extends BaseController
{
    //首页信息
    public function getHomePageInfo()
    {
    	//获取首页轮播图
    	$banners = PictureModel::getBanner();
    	$clouds = CloudLessonModel::getList();
    	$articles = ArticleModel::getList();

    	foreach ($clouds as &$item) {
    		$item->visit_num = $item->look_num + $item->base_num;
    		$item->look_num = 0;
    		$item->base_num = 0;
    	}

    	return $this->outPutSucc([
    		"banners" => $banners,
    		"clouds" => $clouds,
    		"articles" => $articles,
    	]);
    }

    //学生课程列表
    public function getUserLessonList()
    {
        $date_time = request('dateTime');
        if($date_time) {
            //传参了
            $start = Carbon::parse($date_time)->startOfMonth();
            $end   = Carbon::parse($date_time)->endOfMonth();

        } else {
            $start = Carbon::now()->startOfMonth();
            $end   = Carbon::now()->endOfMonth();
        }
        $cur_month_end = Carbon::now()->endOfMonth();
        $user_id = auth()->user()->id;
        // dd($cur_month_end);
        $lesson_list = LessonModel::getUserLessons($user_id, $start, $end);

        $list = [];
        $item = [];
        $next_list = [];
        //整理格式
        foreach ($lesson_list as $val) {
            if($val->cloud_id){
                //云课堂
                $item['img']         = $val->cloudLessons->poster;
                $item['lesson_name'] = $val->cloudLessons->title;
                $item['lesson_tea']  = '云课堂';
            } else {
                $item['img']         = $val->teachers->headimg;
                $item['lesson_name'] = $val->lessonTypes->name;
                $item['lesson_tea']  = '外教：' . $val->teachers->name;
            }

            $item['date'] = date('m/d', strtotime($val->begin_time));
            $item['id']   = $val->id;
            $item['time'] = substr($val->begin_time,11,16) .'-'.  substr($val->end_time,11,16);
            $item['lesson_status']   = $val->lesson_status;

            if( $val->begin_time > now() && $val->begin_time < $cur_month_end) {
                $next_list = $item;
            }
            $day = date('d', strtotime($val->begin_time));
            $list[$day][] = $item;
        }

        return $this->outPutSucc([
            'lesson_list' => $list,
            'next_list'   => $next_list
        ]);
    }


    //用户的外教列表
    public function getMyTeacher()
    {
        $user_id = auth()->user()->id;
        $start = request('start', 0);
        $list = StuCollectModel::getUserTeachers($user_id, $start);

        return $this->outPutSucc($list);
    }


    //用户提交建议
    public function addAdvice()
    {
        $user_id = auth()->user()->id;
        $content = request('content', '');
        // $images  = request('images');
        if($content === "") {
            return $this->outPutErr('请输入内容！');
        }

        //图片上传
        // if($image_flag) {
            $images = request()->file('images')->store();
        // }

        $ret = AdviceModel::create([
            'user_id' => $user_id,
            'content' => $content,
            'images'  => $images
        ]);

        if($ret){

            return $this->outPutSucc();
        }

        return $this->outPutErr('网络错误，提交失败！');
    }


    //获取用户资料
    public function getUserInfo()
    {
        return auth()->user();
    }
}
